# 导包
import unittest
from api.login import LoginAPI
import random
from common.dbutil import DBUtil
import config
from parameterized import parameterized
from common.readjson import read_json_file
from common.readjson import read_json_login
import requests


# 注意：unittest框架下的参数化必须在类的外面执行代码，不能在类的里面直接运行。
# 说明：
# 准备测试数据：
# （1）已注册的账号
#     + 查询数据库，获取已注册账号信息
#     + 直接在注册页面进行注册
#     + 直接运行注册接口获取注册账号信息


# 创建测试类
class TestLoginAPI(unittest.TestCase):
    # 初始化
    cookies = None

    # 类前置处理
    @classmethod
    def setUpClass(cls) -> None:
        # 清理测试数据
        # 用户主表
        # print(DBUtil.exe_sql('DELETE from mb_member where phone = "{}";'.format(config.MOBILE)))
        #
        # # 用户信息表
        # print(DBUtil.exe_sql('DELETE from mb_member_info where member_name = "{}";'.format(config.MOBILE)))
        #
        # # 用户登录日志表
        # print(DBUtil.exe_sql('DELETE from mb_member_login_log where member_name = "{}";'.format(config.MOBILE)))
        #
        # # 用户注册日志表
        # print(DBUtil.exe_sql('DELETE from mb_member_register_log where member_name = "{}";'.format(config.MOBILE)))
        pass

    # 类后置处理
    @classmethod
    def tearDownClass(cls) -> None:
        # 清理测试数据
        # 用户主表
        # print(DBUtil.exe_sql('DELETE from mb_member where phone = "{}";'.format(config.MOBILE)))
        #
        # # 用户信息表
        # print(DBUtil.exe_sql('DELETE from mb_member_info where member_name = "{}";'.format(config.MOBILE)))
        #
        # # 用户登录日志表
        # print(DBUtil.exe_sql('DELETE from mb_member_login_log where member_name = "{}";'.format(config.MOBILE)))
        #
        # # 用户注册日志表
        # print(DBUtil.exe_sql('DELETE from mb_member_register_log where member_name = "{}";'.format(config.MOBILE)))
        pass

    # 前置处理
    def setUp(self) -> None:
        # 打印标识
        print("方法前置处理 ===》")
        # 实例化接口
        self.login_api = LoginAPI()
        self.session = requests.session()

    # 后置处理
    def tearDown(self) -> None:
        # 打印标识
        print("《====方法后置处理")
        self.session.close()

    # # 获取图片验证码成功
    # def test01_get_verify_code(self):
    #     # 产生一个0到1之间的随机数
    #     response = self.login_api.get_verify_code(random.random())
    #
    #     # 产生一个1到100之间的随机数
    #     # response = self.login_api.get_verify_code(random.randint(1, 100))
    #
    #     # 不传递随机数
    #     # response = self.login_api.get_verify_code(None)
    #
    #     # 打印URL信息
    #     print(response.url)
    #
    #     # print(response.text)
    #     print(response.status_code)
    #
    #     # 断言响应状态码为200
    #     self.assertEqual(200, response.status_code)
    #
    #     # 保存cookies信息
    #     TestLoginAPI.cookies = response.cookies
    #
    # def test02_get_phone_code(self):
    #     test_data = {
    #         "phone": config.MOBILE,
    #         "imgVerifyCode": "8888",
    #         "type": "reg"
    #     }
    #     response = self.login_api.get_phone_code(phone_data=test_data, cookies_data=TestLoginAPI.cookies)
    #     print(response.text)
    #
    # @parameterized.expand(read_json_file())
    # def test03_reg(self, phone, password, verifycode, phone_code, dy_server, invite_phone, status, description):
    #     reg_data = {
    #         "phone": phone,
    #         "password": password,
    #         "verifycode": verifycode,
    #         "phone_code": phone_code,
    #         "dy_server": dy_server,
    #         "invite_phone": invite_phone
    #     }
    #     response = self.login_api.reg(reg_data=reg_data, cookies_data=TestLoginAPI.cookies)
    #     print(response.json())
    #     # 断言
    #     # self.assertEqual(200, response.status_code)
    #     self.assertEqual(status, response.json().get("status"))
    #     self.assertIn(description, response.json().get("description"))


    # 参数化
    # @parameterized.expand(read_json_login())
    # def test01_login(self, login_data, status_code, status, description):
    #     response = self.login_api.login(login_data)
    #     print(response.json())
    #     self.assertEqual(status_code, response.status_code)
    #     self.assertEqual(status, response.json().get("status"))
    #     self.assertIn(description, response.json().get("description"))

    def test01_login(self):
        resp = self.login_api.login666(session=self.session, login_data={"keywords": "13137812345","password": "a1008611"})
        print(resp.json())

        # 断言
        self.assertEqual(200,resp.status_code)
        self.assertEqual("登录成功",resp.json().get('description'))




if __name__ == '__main__':
    unittest.main()